package it.poste.il.compensatore.dao.impl;

import java.util.List;
import java.util.Properties;

import it.poste.il.compensatore.dao.MessageDao;
import it.poste.il.compensatore.entities.Message;
import it.poste.il.compensatore.utilities.LoadQueriesSQL;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class MessageDaoImpl extends BaseEntityDaoImpl<Message> implements MessageDao {
    public MessageDaoImpl() {
        super(Message.class, "Message");
    }

    LoadQueriesSQL loadQueriesSQL;
    
	public LoadQueriesSQL getLoadQueriesSQL() {
		return loadQueriesSQL;
	}

	public void setLoadQueriesSQL(LoadQueriesSQL loadQueriesSQL) {
		this.loadQueriesSQL = loadQueriesSQL;
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public int selectOperationId(Message msg) {
		try {
			Properties p = loadQueriesSQL.getProperties();
			String querySQL = p.getProperty("selectOperationId");
			Query query = getEntityManager().createNativeQuery(querySQL, Integer.class).
					setParameter("id", msg.getId()).setParameter("type", msg.getType());
			return (Integer) query.getResultList().get(0);
		} catch (NoResultException e) {
		      return 0;
		}
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public int selectStatusId(Message msg) {
		try {
			Properties p = loadQueriesSQL.getProperties();
			String querySQL = p.getProperty("selectStatusId");
			Query query = getEntityManager().createNativeQuery(querySQL, Integer.class).
					setParameter("id", msg.getId()).setParameter("type", msg.getType());
			return (Integer) query.getResultList().get(0);
		} catch (NoResultException e) {
		      return 0;
		}
	}

	//preleva tutti gli id dei messaggi che hanno assoiato un annullamento type=1
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public List<Message> loadListByType(int type){
		Properties p = loadQueriesSQL.getProperties();
		String querySQL = p.getProperty("loadListByType");
		Query query = getEntityManager().createNativeQuery(querySQL, Message.class).
				setParameter("type", type);
		List<Message> msgs = query.getResultList();
		return msgs;
	}
	
	public Message loadByRequestId(Long id){
		return null;
	}

//	@Transactional
//	public int selectMessageId(Message msg) {
//		try {
//	      return (int) getEntityManager().createNamedQuery("SELECT msg.id FROM Messages msg " +
//              "WHERE msg.requestid = :requestId AND msg.operationid = :operationId").
//              setParameter("requestId", msg.getRequestId()).
//              setParameter("operationId", msg.getOperationId()).
//              getSingleResult();
//	    } catch (NoResultException e) {
//	      return 0;
//	    }
//	}
//	
//	@Transactional
//	public int loadLastMessage(Message message){
//		try{
//			return (Integer) getEntityManager().createNamedQuery("SELECT msg.statusId_id FROM Messages msg,  " +
//		    		  "WHERE msg.id = :id AND msg.type = :type").
//		              setParameter("id", message.getId()).
//		              setParameter("type", message.getType()).
//	              getSingleResult();
//		} catch (NoResultException e) {		
//			return  0;
//		}
//	}
//	
//	@Transactional
//		public Long loadLastMessage(Message message) throws DataAccessException {
//	        Query query = getEntityManager().createQuery("select id from Message obj order by obj.timestamp desc");
//	        //noinspection unchecked
//			List<Long> resultList = query.getResultList();
//	        return resultList.get(0);
//	    }
	
}
